package com.kamkor.interviewer.client.presenter;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;
import com.kamkor.interviewer.client.InterviewServiceAsync;
import com.kamkor.interviewer.client.Interviewer;
import com.kamkor.interviewer.client.LoadingIndicator;
import com.kamkor.interviewer.client.security.Authentication;
import com.kamkor.interviewer.shared.InterviewDetails;
import com.kamkor.interviewer.shared.QuestionAnswerDetails;

/**
 * 
 * @author kamkor
 *
 */
public class MergeInterviewPresenter extends SecuredPresenter {
	private final InterviewServiceAsync rpcService;
	private final Display display;
	private final HandlerManager eventBus;	
	
	public interface Display {		
		RichTextArea getMergedInterviewArea();
		HasClickHandlers getShowHtmlText();
		void setData(String interviewHtml);
		void setShowHtmlTextTitle(String title);
		String getShowHtmlTextTitle();
		Widget asWidget();	
	}
	
	public MergeInterviewPresenter(InterviewServiceAsync rpcService, Display display, HandlerManager eventBus, Long interviewId, 
			Long adminKey, Authentication authentication) {
		super(authentication, interviewId, adminKey);
		this.rpcService = rpcService;
		this.display = display;
		this.eventBus = eventBus;
	}
	
	public void bind() {		
		display.getShowHtmlText().addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				doShowHtmlOrText();
			}			
		});
	}
	
	@Override
	protected void goAuthenticated(HasWidgets container) {
		bind();
		container.clear();	
		container.add(display.asWidget());
		fetchInterview(getInterviewId());	
	}
	
	/**
	 * 
	 * @param interviewId
	 */
	private void fetchInterview(Long interviewId) {
		LoadingIndicator.loadingStarted(Interviewer.constants.loading());
		rpcService.getInterview(interviewId, new AsyncCallback<InterviewDetails>() {
			@Override
			public void onFailure(Throwable caught) {
				LoadingIndicator.loadingFinished();	
			}
			@Override
			public void onSuccess(InterviewDetails result) {
				fetchQuestionAnswer(result);
				LoadingIndicator.loadingFinished();	
			}			
		});
	}
	
	/** 
	 * Load QuestionAnswer from database and add them to flexTable 
	 */
	private void fetchQuestionAnswer(final InterviewDetails interviewDetails) {
		LoadingIndicator.loadingStarted(Interviewer.constants.loading());
		rpcService.getQuestionAnswer(getInterviewId(), new AsyncCallback<List<QuestionAnswerDetails>>() {			
			public void onFailure(Throwable caught) {
				LoadingIndicator.loadingFinished();	
			}		
			public void onSuccess(List<QuestionAnswerDetails> result) {	
				StringBuffer sb = new StringBuffer();
				if (result.size() != 0) {
					QuestionAnswerDetails firstQa = result.get(0);
					sb.append("<b>" + interviewDetails.getInterviewerName() + ": " + firstQa.getQuestion() + "<br/><br/></b>");
					sb.append("<b>" + interviewDetails.getInterviewedName() + ":</b> " + firstQa.getAnswer() + "<br/><br/>");
					
					result = result.subList(1, result.size());
					for(QuestionAnswerDetails qa : result) {
						sb.append("<b>" + interviewDetails.getInterviewerInitials() + ": " + qa.getQuestion() + "<br/><br/></b>");
						sb.append("<b>" + interviewDetails.getInterviewedInitials() + ":</b> " + qa.getAnswer() + "<br/><br/>");	
					}				
					display.setData(sb.toString());				
					LoadingIndicator.loadingFinished();	
				}
			}			
		});				
	}
	
	/**
	 * It shows shows html or text in RichTextArea and swaps button label to ShowHtml and ShowText
	 * 
	 * If text area contains html, show text and change button label to show html	  
	 * If text area contains text, show html and change button label to show text	  
	 * 
	 */
	private void doShowHtmlOrText() {
		String labelText = display.getShowHtmlTextTitle();
		RichTextArea area = display.getMergedInterviewArea();	
		if(labelText.equals("html")) {
			area.setText(area.getHTML());
			display.setShowHtmlTextTitle("text");
		} else if(labelText.equals("text")) {	
			area.setHTML(area.getText());
			display.setShowHtmlTextTitle("html");
		}		
	}

	@Override
	public void authenticationFailed() {
				
	}

}
